//
//  LoginViewController.m
//  HTP
//
//  Created by 陈钱钿 on 15/11/6.
//  Copyright © 2015年 nit. All rights reserved.
//

#import "LoginViewController.h"
#import "AppDelegate.h"
#define kBorder 20
#define height 40
@interface LoginViewController () <UITextFieldDelegate>

@end

@implementation LoginViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationController.navigationBarHidden = NO;
    self.view.backgroundColor = [UIColor colorWithRed:233.0/255 green:233.0/255 blue:233.0/255 alpha:1];
    [self initUI];
    
    [self addCustomBackWithAction:@selector(doBack) normalImg:[UIImage imageNamed:@"back"] selectImg:[UIImage imageNamed:@"back"] withPadding:5];
    
    [self addCustomTitleWithTitle:@"登录"];
    
}



//TODO:绘制UI
- (void)initUI {
    
    UIView *userView = [[UIView alloc] initWithFrame:CGRectMake(15, 2*kBorder, ScreenWidth-2*15, height)];
    UIImageView *inputImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth-2*15, height)];
    inputImageView.image = [UIImage imageNamed:@"bg_input"];
    [userView addSubview:inputImageView];
    UIImageView *userIcon = [[UIImageView alloc] initWithFrame:CGRectMake(10, 10, 18, 20)];
    userIcon.image = [UIImage imageNamed:@"username"];
    [userView addSubview:userIcon];
    UIImageView *sline = [[UIImageView alloc] initWithFrame:CGRectMake(CGRectGetMaxX(userIcon.frame)+5, 10, 1, height-2*10)];
    sline.image = [UIImage imageNamed:@"line_input"];
    [userView addSubview:sline];
    UITextField *userTextField = [[UITextField alloc] initWithFrame:CGRectMake(CGRectGetMaxX(sline.frame)+10, 0, 230, height)];
    userTextField.placeholder = @"请输入用户名";
    userTextField.delegate = self;
    [userView addSubview:userTextField];
    [self.view addSubview:userView];
    
    UIView *pwdView = [[UIView alloc] initWithFrame:CGRectMake(15, CGRectGetMaxY(userView.frame)+2*kBorder, ScreenWidth-2*15, height)];
    UIImageView *inputImageView2 = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth-2*15, height)];
    inputImageView2.image = [UIImage imageNamed:@"bg_input"];
    [pwdView addSubview:inputImageView2];
    UIImageView *pwdIcon = [[UIImageView alloc] initWithFrame:CGRectMake(10, 10, 18, 20)];
    pwdIcon.image = [UIImage imageNamed:@"password"];
    [pwdView addSubview:pwdIcon];
    UIImageView *sline2 = [[UIImageView alloc] initWithFrame:CGRectMake(CGRectGetMaxX(pwdIcon.frame)+5, 10, 1, height-2*10)];
    sline2.image = [UIImage imageNamed:@"line_input"];
    [pwdView addSubview:sline2];
    UITextField *pwdTextField = [[UITextField alloc] initWithFrame:CGRectMake(CGRectGetMaxX(sline2.frame)+10, 0, 230, height)];
    pwdTextField.placeholder = @"请输入登录密码";
    pwdTextField.secureTextEntry = YES;
    pwdTextField.delegate = self;
    [pwdView addSubview:pwdTextField];
    [self.view addSubview:pwdView];
    
    UIButton *confirmBtn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    confirmBtn.frame = CGRectMake(15, CGRectGetMaxY(pwdView.frame)+2*kBorder, ScreenWidth-2*15, height+2);
    [confirmBtn setBackgroundImage:[UIImage imageNamed:@"bg_bt"] forState:UIControlStateNormal];
    [confirmBtn addTarget:self action:@selector(confirmClick) forControlEvents:UIControlEventTouchUpInside];
    [confirmBtn setTitle:@"确 认" forState:UIControlStateNormal];
    [confirmBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [self.view addSubview:confirmBtn];
    
}

//TODO:确认按钮的点击
- (void)confirmClick {
    [self changeRoot];
}

/*
 更改RootViewController
 */
-(void)changeRoot{
    AppDelegate *app = (AppDelegate *)[[UIApplication sharedApplication] delegate];
    [app changeRootVc];
}

- (void)doBack {
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    [self.view endEditing:YES];
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField {
    [self.view endEditing:YES];
    return YES;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
